package com.system.controller;

import com.system.entity.*;
import com.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.awt.image.ImageProducer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RequestMapping("/")
@Controller
public class TeacherController {
    @Autowired
    private teacherService teacherService;
    @Autowired
    private teachCourseService teachCourseService;
    @Autowired
    private courseService courseService;
    @Autowired
    private courseInformationService courseInformationService;
    @Autowired
    private takeCourseService takeCourseService;
    @RequestMapping("teachercourse")
    @ResponseBody
    public List<courseList> result_course(@RequestBody Map<String,String> map){
        courseList courseList1 = new courseList("1-2节");
        courseList courseList2 = new courseList("3-4节");
        courseList courseList3 = new courseList("5-6节");
        courseList courseList4 = new courseList("7-8节");
        courseList courseList5 = new courseList("9-10节");
        courseList courseList6 = new courseList("11-13节");
        List<courseList> C_list = new ArrayList<>();
        Long tea_id = Long.valueOf(map.get("user_id"));
        List<Teachcourse> result = teachCourseService.findByTeacherId(tea_id);
        Iterator<Teachcourse> iter = result.iterator();
        Teachcourse tmp;
        while(iter.hasNext()){
            tmp = iter.next();
            CourseInformation courseInformation = courseInformationService.selectByPrimaryKey(tmp.getCourseId());
            String coursename = courseService.findByCourseCoding(courseInformation.getCourseCoding()).getCourse_name();
            String grade =courseInformation.getTeachgrade();
            String weekname = courseInformation.getDaytime().toString();
            String place = courseInformation.getCoursePlace().toString();
            String week  = courseInformation.getWeek();
            if (courseInformation.getCourseTime().equals("1-2")){
                setWeek(week,courseList1,coursename,grade,weekname,place);
            }
            else if (courseInformation.getCourseTime().equals("3-4")){
                setWeek(week,courseList2,coursename,grade,weekname,place);
            }
            else if (courseInformation.getCourseTime().equals("5-6")){
                setWeek(week,courseList3,coursename,grade,weekname,place);
            }
            else if (courseInformation.getCourseTime().equals("7-8")){
                setWeek(week,courseList4,coursename,grade,weekname,place);
            }
            else if (courseInformation.getCourseTime().equals("9-10")){
                setWeek(week,courseList5,coursename,grade,weekname,place);
            }
            else {
                setWeek(week,courseList6,coursename,grade,weekname,place);
            }
        }
        C_list.add(courseList1);
        C_list.add(courseList2);
        C_list.add(courseList3);
        C_list.add(courseList4);
        C_list.add(courseList5);
        C_list.add(courseList6);
        return C_list;
    }

    /**
     * 录入成绩页面
     * @param map
     * @return
     */
    @RequestMapping("teacherentergrade")
    @ResponseBody
    public List<Entergrade> result_loggingGrade(@RequestBody Map<String , String> map){
        Long tea_id = Long.valueOf(map.get("user_id"));
        String semester = map.get("semester");
        List<Entergrade> result = teachCourseService.findBySemester(tea_id,semester);
        for(Entergrade e : result){
            if(e.getEnteredStudentNumber().equals(e.getTotalStudentNumber())){
                result.remove(e);
            }
        }
        return result;
    }
    @RequestMapping("teacherallgrade")
    @ResponseBody
    public List<Entergrade> result_AllGrade(@RequestBody Map<String , String> map){
        Long tea_id = Long.valueOf(map.get("user_id"));
        String semester = map.get("semester");
        List<Entergrade> result = teachCourseService.findBySemester(tea_id,semester);
        return result;
    }

    @RequestMapping("updategrade")
    @ResponseBody
    public List<Entergrade> result_Grade(@RequestBody Map<String , String> map){
        Long tea_id = Long.valueOf(map.get("user_id"));
        String semester = map.get("semester");
        List<Entergrade> result = teachCourseService.findBySemester(tea_id,semester);
        for(Entergrade e : result){
            if(e.getEnteredStudentNumber().equals("0")){
                result.remove(e);
            }
        }
        return result;
    }

    @RequestMapping("enter")
    @ResponseBody
    public int kk(@RequestBody Map<String , String> map){
        Long stu_id = Long.valueOf(map.get("stu_id"));
        Integer course_id = Integer.valueOf(map.get("course_id"));
        Integer usual_performance = Integer.valueOf(map.get("usual_performance"));
        Integer final_performance = Integer.valueOf(map.get("final_performance"));
        Integer experience = Integer.valueOf(map.get("experience"));
        Integer total_performance = Integer.valueOf(map.get("total_performance"));
        Takecourse takecourse = new Takecourse();
        takecourse.setStu_id(stu_id);
        takecourse.setCourseId(course_id);
        takecourse.setUsualPerformance(usual_performance);
        takecourse.setExperience(experience);
        takecourse.setFinalPerformance(final_performance);
        takecourse.setTotalPerformance(total_performance);
        return takeCourseService.update(takecourse);
    }
    /**
     * 录入成绩
     * @param map
     * @return
     */
    @RequestMapping("enterupdategrade")
    @ResponseBody
    public  List<Takecourse> result_updategrade(@RequestBody Map<String,String> map){
        Long tea_id = Long.valueOf(map.get("user_id"));
        String semester = map.get("semester");
        Integer course_id = Integer.valueOf(map.get("course_id"));
        List<Takecourse> takecourse = teachCourseService.findByupdateStudentGrade(course_id);
        return takecourse;
    }
    @RequestMapping("allgrade")
    @ResponseBody
    public  List<Takecourse> result_allgrade(@RequestBody Map<String,String> map){
        Long tea_id = Long.valueOf(map.get("user_id"));
        String semester = map.get("semester");
        Integer course_id = Integer.valueOf(map.get("course_id"));
        List<Takecourse> takecourse = teachCourseService.findByupdateStudentGrade(course_id);
        takecourse.addAll(teachCourseService.findByEveryStudentGrade(course_id));
        return takecourse;
    }
    @RequestMapping("entergrade")
    @ResponseBody
    public  List<Takecourse> result_grade(@RequestBody Map<String,String> map){
        Long tea_id = Long.valueOf(map.get("user_id"));
        String semester = map.get("semester");
//        Integer course_id = 0;
//        if(!map.get("course_id").equals(""))course_id = Integer.valueOf(map.get("course_id"));
//        String courseType = map.get("courseType");
//        String courseName = map.get("courseName");
//        String detailSearch = map.get("detailSearch");
        Integer course_id = Integer.valueOf(map.get("course_id"));
        List<Takecourse> takecourse = teachCourseService.findByEveryStudentGrade(course_id);
//        if (isSemester(semester)&&isCourseID(course_id)){
//            entergrade = teachCourseService.findBySemId(tea_id,semester,course_id);
//        }
//        else if (isSemester(semester)&&isCourseType(courseType)){
//            entergrade = teachCourseService.findBySemType(tea_id,semester,courseType);
//        }
//        else if (isSemester(semester)&&isCourseName(courseName)){
//            entergrade = teachCourseService.findBySemName(tea_id,semester,courseName);
//        }
//        else if (isCourseName(courseName)){
//            entergrade = teachCourseService.findByCourseName(tea_id,courseName);
//        }
//        else if (isSemester(semester)){
//            entergrade = teachCourseService.findBySemester(tea_id,semester);
//        }
//        else if (isCourseType(courseType)){
//            entergrade = teachCourseService.findByCourseType(tea_id,courseType);
//        }
//        else if (isCourseID(course_id)){
//            entergrade = teachCourseService.findByCourseID(tea_id,course_id);
//        }
//        else {
//            entergrade=  teachCourseService.findBytea_Id(tea_id);
//        }
        return takecourse;
    }
    @RequestMapping("sjjjjj")
    @ResponseBody
    public void setGrade(@RequestBody Map<String , String >map){
        String stu_id = map.get("stu_id");

    }
    public void setWeek(String week,courseList courselist,String coursename
            ,String grade,String weekname,String place){
        if (weekname.equals("1")){
            courselist.setMonday(coursename+"\n"+grade
                    +"\n"+week+"\n"+place);
        }
        else if (weekname.equals("2")){
            courselist.setTuesday(coursename+"\n"+grade
                    +"\n"+week+"\n"+place);
        }
        else if (weekname.equals("3")){
            courselist.setWednesday(coursename+"\n"+grade
                    +"\n"+week+"\n"+place);
        }
        else if (weekname.equals("4")){
            courselist.setThursday(coursename+"\n"+grade
                    +"\n"+week+"\n"+place);
        }
        else if (weekname.equals("5")){
            courselist.setFriday(coursename+"\n"+grade
                    +"\n"+week+"\n"+place);
        }
        else if (weekname.equals("6")){
            courselist.setSaturday(coursename+"\n"+grade
                    +"\n"+week+"\n"+place);
        }
        else if (weekname.equals("7")){
            courselist.setSunday(coursename+"\n"+grade
                    +"\n"+week+"\n"+place);
        }

    }
    public boolean isSemester(String semester){
        if (semester.equals("")){
            return false;
        }
        return true;
    }
    public boolean isCourseID(Integer course_id){
        if (course_id==0){
            return false;
        }
        return true;
    }
    public boolean isCourseName(String course_name){
        if (course_name.equals("")){
            return false;
        }
        return true;
    }
    public boolean isCourseType(String courseType){
        if (courseType.equals("")){
            return false;
        }
        return true;
    }
}
